/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package udp2serial;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.awt.Color;
import java.io.*;
import java.util.*;


/**
 *
 * @author axelsben
 */
public class rx {
    private String useComport = "COM1";
    private javax.swing.JTextArea jta = null;
    private srvThread srvT = null;
    private boolean running = true;
    private String useBaudrate = "9600";
    private InputStream in;
    private OutputStream out;
    private boolean logging = false;
    private boolean newline = false;


    public void setNewLine(boolean bool)
    {
        newline = bool;
    }


    public void setLogging(boolean bool)
    {
        logging = bool;
    }

    public void setTA(javax.swing.JTextArea jtxta)
    {
        jta = jtxta;
    }

    public void setBaudrate(String baudrate)
    {
        useBaudrate = baudrate;
    }

    public String getBaudrate()
    {
        return useBaudrate;
    }

    public void setComport(String comport)
    {
        useComport = comport;
    }

    public String getComport()
    {
        return useComport;
    }

    void connect () throws Exception
    {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(useComport);
        if ( portIdentifier.isCurrentlyOwned() )
        {
            System.out.println("Error: Port is currently in use");
        }
        else
        {
            CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);

            if ( commPort instanceof SerialPort )
            {
                int baud = Integer.parseInt(useBaudrate);
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(baud,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

                in = serialPort.getInputStream();
                out = serialPort.getOutputStream();
                tx.setOutputStrem(out);
            }
            else
            {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }
    }


    public void startServer () throws IOException
    {
        running = true;
        tx.setTA(jta);
        tx.setBaudrate(useBaudrate);
        tx.setComport(useComport);
        try
        {
            srvT = new srvThread();
            srvT.start();
        }
        catch(Exception x)
        {

        }
    }

    public void stopServer()
    {
        running = false;
        srvT = null;
        System.gc();
    }

    public class srvThread extends Thread
    {

        public srvThread() throws IOException
        {
            this("srvThreadRS232");
        }

        public srvThread(String name) throws IOException
        {
            super(name);

        }



        @Override
        public void run() {
            int len = 0;
            byte[] buffer = new byte[1024];
            String recStr = "";
            boolean sendIt = false;

            while (running) {
                try {
                    byte[] buf = new byte[256];

                    // receive request
                    while ( (len = in.read(buffer)) > -1 )
                    {
                        String tmp = new String(buffer,0,len);
                        recStr += tmp;
			//System.out.println(recStr);
                        if(newline && recStr.length() > 0)
                        {
                            sendIt = false;
                            if(recStr.indexOf(10)>=0)
                            {
                                sendIt = true;
                            }
                            if(recStr.indexOf(13)>=0)
                            {
                                sendIt = true;
                            }
                        }

                        if(!newline)
                        {
                            sendIt = true;
                        }

			if((recStr.length() > 0) && (sendIt))
			{
                            String[] tmpStr = new String[1];
                            tmpStr[0] = new String(recStr);
                            //txRS232.send(tmpStr);
                            client.send(tmpStr);
                            if(logging)
                            {
                                jta.setSelectionStart(jta.getText().length());
                                jta.setSelectionEnd(jta.getText().length()+1);
                                jta.replaceSelection("/sr:" + recStr);
                                if(jta.getText().length() > 25000)
                                {
                                    int rm = jta.getText().length() - 25000;
                                    jta.setSelectionStart(0);
                                    jta.setSelectionEnd(rm);
                                    jta.replaceSelection("");
                                }
                                jta.setSelectionStart(jta.getText().length());
                                jta.setSelectionEnd(jta.getText().length()+1);
                            }
                            recStr = "";

                        }

                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
